/**
 * Simple link control.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

import core.ui.FocusWindow;
import core.util.Listeners;

class Link extends FocusWindow {

	/**
	 * Creates new link control.
	 * 
	 * @param parent (Window) [optional] parent window
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] control size.
	 */
	this.Link = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);
		this.listeners = new Listeners(['onActionPerformed']);
		this.setStyle('display', 'block');
		this.setTarget(Link.TARGET_SELF);
		// FIXME: javascript:void(0) will tell IE that window is closing
		// and it will fire onbeforeunload event.
		this.setHref('#');
	}
	
	/**
	 * Sets the link URL.
	 *
	 * @param href (string) link url.
	 */
	this.setHref = function(href) {
		this.getElement().href = href;
	}
	
	/**
	 * Returns link URL.
	 *
	 * @return string
	 */
	this.getHref = function() {
		return this.getElement().href;
	}

	/**
	 * Adds a new action listener.
	 *
	 * @param listener (object) The listener to add.
	 */
	this.addActionListener = function(listener) {
		this.listeners.add(listener);
	}

	/**
	 * Removes the action listener.
	 *
	 * @param listener (object) The listener to remove.
	 */
	this.removeActionListener = function(listener) {
		this.listeners.remove(listener);
	}

	/**
	 * @private
	 * Fires when the click is performed.
	 */
	this.fireActionPerformed = function() {
		this.listeners.call('onActionPerformed', [ this ]);
	}
	
	/**
	 * Sets link target.
	 * The target can be one of the following:
	 * Link.TARGET_BLANK
 	 * Link.TARGET_PARENT
 	 * Link.TARGET_SELF
 	 * Link.TARGET_TOP
	 * 
	 * @param target (string) target value.
	 */
	this.setTarget = function(target) {
		this.getElement().target = target;
	}
	
	/**
	 * Returns current link target.
	 *
	 * @return string
	 */
	this.getTarget = function() {
		return this.getElement().target;
	}
	
	/**
	 * @protected
	 * Creates and returns root element for link control.
	 *
	 * @return HTMLAnchorElement
	 */
	this.createElement = function() {
		return document.createElement('a');
	}

	this.onclick = function() {
		this.fireActionPerformed();
	}
}

core.ui.Link.TARGET_BLANK = '_blank';
core.ui.Link.TARGET_PARENT = '_parent';
core.ui.Link.TARGET_SELF = '_self';
core.ui.Link.TARGET_TOP = '_top';